package com.awesumgames.awesum;

import tv.ouya.console.api.OuyaController;

import com.awesumgames.awesum.AwesumGlobal.Awesum;
import com.awesumgames.awesum.controls.Control.Touch;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;

public abstract class AwesumActivity extends Activity implements Game.GameListener {
	private GestureDetector gestures;
    private SensorManager mSensorManager;
	private Sensor mAccelerometer;
    final float[] mValuesAccel = new float[3];
    private AwesumView awesumView;
    private int _resourceActivity = -1, _resourceView = -1;
    
	public AwesumActivity(int resActivity, int resView) {
		_resourceActivity = resActivity;
		_resourceView = resView;
	}

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(_resourceActivity);
		//Awesum.debug("aaaaaaaa===" +getCurrentFocus().toString());
		OuyaController.init(this);
		
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(mEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        Awesum.setup(getApplicationContext());
        gestures = new GestureDetector(Awesum.context, new GestureListener());
        awesumView = (AwesumView)findViewById(_resourceView);
        Game.addListener(this);
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent e) {
		switch (e.getAction()) {
			case MotionEvent.ACTION_MOVE:
				Touch.move = true;
				Touch.pos.set(e.getX(), e.getY());
				break;
			case MotionEvent.ACTION_DOWN:
				Touch.hold = true;
				Touch.pos.set(e.getX(), e.getY());
				break;
			case MotionEvent.ACTION_UP:
				Touch.hold = false;
				Touch.pos.set(e.getX(), e.getY());
				break;
		}
        return gestures.onTouchEvent(e);
    }
	
	@Override
	protected void onResume() {
		//Awesum.debug("onresume");
		super.onResume();
		mSensorManager.registerListener(mEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
		//Awesum.debug("end of onresume");
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_LEFT:
				Awesum.debug("left");
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				Awesum.debug("right");
				break;
			default:
			    boolean handled = false;
			    synchronized (Game.pauseInput) {
			        handled = OuyaController.onKeyDown(keyCode, event);
			        //findOrCreatePlayer(event.getDeviceId());
			    }
			    
				if (keyCode == OuyaController.BUTTON_A)
				    finish();
				
				return handled || super.onKeyDown(keyCode, event);
		}
		return false;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_LEFT:
				//Awesum.debug("left");
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				//Awesum.debug("right");
				break;
			default:
			    boolean handled = false;
			    synchronized (Game.pauseInput) {
			        handled = OuyaController.onKeyUp(keyCode, event);
			    }
			    return handled || super.onKeyUp(keyCode, event);
		}
		return false;
	}

	@Override
	public boolean onGenericMotionEvent(MotionEvent event) {
		boolean handled = false;
	    synchronized (Game.pauseInput) {
	        handled = OuyaController.onGenericMotionEvent(event);
	    }
        return handled || super.onGenericMotionEvent(event);
	}
	
	final SensorEventListener mEventListener = new SensorEventListener() {
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

		@Override
        public void onSensorChanged(SensorEvent event) {
            // Handle the events for which we registered
            switch (event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                    if (awesumView != null)
                    	awesumView.setAccelerometerData(mValuesAccel);
                    //Awesum.debug("accelerometer: " + Float.toString(mValuesAccel[0]) + ", " + Float.toString(mValuesAccel[1]) + ", " + Float.toString(mValuesAccel[2]));
                    break;

                /*case Sensor.TYPE_MAGNETIC_FIELD:
                    System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                    break;*/
            }
        }
    };
	
	private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
        	//Awesum.debug("double tapped");
        	Touch.doubleTap = true;
        	Touch.pos.set(e.getX(), e.getY());
            return true;
        }
    }
	
	public abstract void ready();
}
